<?php

namespace api\modules\v1\forms;

use common\models\api\AccessToken;
use common\models\member\Member;
use Yii;
use yii\base\Model;

/**
 * Class LoginForm
 * @package api\modules\v1\forms
 *
 */
class LoginForm extends Model
{
    /**
     * @var string $username 用户名
     * @var string $password 密码
     * @var string $group 组别【登陆终端】
     */
    public $username;
    public $password;
    public $group;

    /**
     * @var Member $_member 用户
     * @var AccessToken $_accessTokenInfo 用户
     */
    private $_member;
    private $_accessTokenInfo;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['username', 'password', 'group'], 'required'],
            ['password', 'checkPassword'],
            ['group', 'checkGroup']
        ];
    }

    public function attributeLabels()
    {
        return [
            'username' => '帐号',
            'password' => '密码',
            'group' => '组别',
        ];
    }


    /**
     * 验证账号密码
     * @param $attribute
     */
    public function checkPassword($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }

        $user = $this->getMember();
        if (empty($user)) {
            $this->addError($attribute, '账号不存在');
            return;
        }

        if ($user->status == Member::STATUS_DELETE || $user->status == Member::STATUS_INVALID) {
            $this->addError($attribute, '账号被禁用');
            return;
        }

        if (Yii::$app->security->validatePassword($this->password, $user->password_hash) === false) {
            $this->addError($attribute, '账号或者密码错误');
        }
    }

    /**
     * 验证账号密码
     * @param $attribute
     * @return void
     */
    public function checkGroup($attribute)
    {
        if ($this->hasErrors()) {
            return;
        }

        $groups = AccessToken::$groups;
        if (isset($groups[$this->group]) === false) {
            $this->addError($attribute, '登陆终端异常');
            return;
        }
    }

    /**
     * 用户信息
     * @return Member|null
     */
    public function getMember(): ?Member
    {
        if (empty($this->_member)) {
            // email 登录
//            if (strpos($this->username, "@")) {
//                $this->_user = Member::findOne(['email' => $this->username, 'status' => 1]);
//            } else {
                $this->_member = Member::findOne([
                    'mobile' => $this->username,
                    'status' => [Member::STATUS_INVALID, Member::STATUS_ENABLE]
                ]);
//            }
        }
        return $this->_member;
    }

    /**
     * 获取用户密钥信息
     * @return AccessToken|null
     */
    public function getAccessToken(): ?AccessToken
    {
        if (empty($this->_accessTokenInfo)) {
            $member = $this->getMember();
            $this->_accessTokenInfo = AccessToken::findOne([
                'member_id' => $member->id,
                'group' => $this->group
            ]);
        }
        return $this->_accessTokenInfo;
    }
}
